package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.config.MyBatisPlusPageConfig;
import com.hqyj.pojo.PaperQuestion;
import com.hqyj.dao.PaperQuestionDao;
import com.hqyj.pojo.ResultVo;
import com.hqyj.service.IPaperQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-09-14
 */
@Service
public class PaperQuestionServiceImpl extends ServiceImpl<PaperQuestionDao, PaperQuestion> implements IPaperQuestionService {

    @Resource
    PaperQuestionDao paperQuestionDao;
    @Autowired
    MyBatisPlusPageConfig myBatisPlusPageConfig;


    //分页查询
    @Override
    public ResultVo queryQuetionByPage(PaperQuestion p) {
        ResultVo rv = new ResultVo();
        //创建分页对象
        Page<PaperQuestion> page = new Page<>(p.getPage(),p.getRow());
        //查询分页
        QueryWrapper<PaperQuestion> queryWrapper = new QueryWrapper<>();
        //根据题目的模糊查询
        queryWrapper.like(p.getQuContent() != null && !p.getQuContent().equals(""),
                "qu_content",
                p.getQuContent());

        //根据题型的精确查询
        queryWrapper.like(p.getQuType() != null && !p.getQuType().equals(""),
                "qu_type",
                p.getQuType());
        //倒序
        queryWrapper.orderByDesc("create_time");

        //分页、条件的组合查询
        Page<PaperQuestion> questionPage = paperQuestionDao.selectPage(page, queryWrapper);

        //判断是否有结果
        if (questionPage.getRecords().size() > 0){
            //记录分页数据
            Map<String,Object> map = new HashMap<>();
            //当前页集合
            map.put("list",questionPage.getRecords());
            //总条数
            map.put("total",questionPage.getTotal());
            //总页数
            map.put("totalPage",questionPage.getPages());
            rv.setSuccess(true);
            rv.setData(map);


        }


        return rv;
    }

    @Override
    public ResultVo add(PaperQuestion p) {

        ResultVo rv = new ResultVo();
        //查重名
        QueryWrapper<PaperQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("qu_content",p.getQuContent());
        PaperQuestion paperQuestion = paperQuestionDao.selectOne(queryWrapper);
        if (paperQuestion != null){
            rv.setInfo("题目重名");
            return rv;
        }
        //获取系统当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        p.setCreateTime(sdf.format(new Date()));
        //数据新增
        int num = paperQuestionDao.insert(p);
        if (num > 0){
            rv.setSuccess(true);
            rv.setInfo("新增成功");
        }else {
            rv.setInfo("新增失败");
        }

        return rv;
    }

    @Override
    public ResultVo update(PaperQuestion p) {

        ResultVo rv = new ResultVo();


        return rv;
    }

    @Override
    public ResultVo del(Integer id) {

        ResultVo rv = new ResultVo();
        QueryWrapper<PaperQuestion> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("qu_id",id);

        int num = paperQuestionDao.delete(queryWrapper);
        if (num > 0){
            rv.setSuccess(true);
            rv.setInfo("删除成功");
        }else {
            rv.setInfo("删除失败");
        }


        return rv;
    }
}
